define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'batch/index' + location.search,
                    add_url: 'batch/add',
                    edit_url: 'batch/edit',
                    del_url: 'batch/del',
                    multi_url: 'batch/multi',
                    import_url: 'batch/import',
                    dragsort_url: '',
                    table: 'batch',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),sortable:true },
                        // {field: 'last_batch_id', title: __('Last_batch_id')},
                        {field: 'factory_id', title: __('Factory_id'), sortable:true },
                        {field: 'group_id', title: __('Group_id'), sortable:true },
                        {field: 'name', title: __('Name'), operate: 'LIKE', sortable:true },
                        {field: 'pinxi_id', title: __('Pinxi_id'), sortable:true },
                        // {field: 'user_id', title: __('User_id')},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden'),"deleted":__('Deleted'),"locked":__('Locked')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'),sortable:true, formatter: Table.api.formatter.datetime, datetimeFormat: 'YYYY-MM-DD', operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // {field: 'updatetime', title: __('Updatetime'), formatter: Table.api.formatter.datetime, datetimeFormat: 'YYYY-MM-DD', operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // 批次结束时间  todo
                        // {field: 'weigh', title: __('Weigh'), operate: false, sortable:true },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {name: 'child1', text: '负责人', extend:'data-toggle="tooltip"', title: '负责人', icon: '', classname: 'btn btn-xs btn-info btn-dialog',
                                    url: Controller.api.formatter.viewlist
                                }
                            ] }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'batch/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'batch/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'batch/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            // id="c-admin_ids" data-source="auth/admin/index" data-params='{"custom[factory_id]":"1"}'  data-field="nickname"

            // todo 131
            // 添加筛选条件,  限制本厂管理员

            // @demo  联动

            // 组别
            $("#c-group_id").data("params", function (obj) {
                return {custom: {factory_id: $("#c-factory_id").val()}};
            });

            // 父级批次
            $("#c-last_batch_id").data("params", function (obj) {
                return {custom: {factory_id: $("#c-factory_id").val()}};
            });

            // 操作员
            $("#c-admin_ids").data("params", function (obj) {
                return {custom: {factory_id: $("#c-factory_id").val()}};
            });

            Controller.api.bindevent();
        },
        edit: function () {
            // 组别
            $("#c-group_id").data("params", function (obj) {
                return {custom: {factory_id: $("#c-factory_id").val()}};
            });

            // 父级批次
            $("#c-last_batch_id").data("params", function (obj) {
                return {custom: {factory_id: $("#c-factory_id").val()}};
            });

            // 操作员
            $("#c-admin_ids").data("params", function (obj) {
                return {custom: {factory_id: $("#c-factory_id").val()}};
            });

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                viewlist: function (value, row, index) {
                    var url = "/wMOsQTWmqR.php/batchrelate?batch_id=" + value.id ;
                    return url;
                }
            }
        }
    };
    return Controller;
});
